package com.atguigu.spzx.manager.demo;

import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.errors.*;

import java.io.FileInputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * @author wx
 * @version 1.0
 * @description: TODO
 * @date 2024/11/13 14:46
 */
public class FileUploadTest {
    public static void main(String[] args) throws Exception {
        // 创建一个Minio的客户端对象
        MinioClient minioClient = MinioClient.builder()
                .endpoint("http://116.205.169.51:9001")
                .credentials("admin", "admin123456")
                .build();

        boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket("spzx-bucket").build());

        // 如果不存在，那么此时就创建一个新的桶
        if(!found){
            minioClient.makeBucket(MakeBucketArgs.builder().bucket("spzx-bucket").build());
        } else{  // 如果存在打印信息
            System.out.println("Bucket 'spzx-bucket' already exists.");
        }

        FileInputStream fis = new FileInputStream("D://1.jpg");
        PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                .bucket("spzx-bucket")
                .stream(fis, fis.available(), -1)
                .object("01.jpg")
                .contentType("image/jpeg")
                .build();
        minioClient.putObject(putObjectArgs);

        // 构建fileUrl
        String fileUrl = "http://116.205.169.51:9001/spzx-bucket/1.jpg";
        System.out.println(fileUrl);
    }


}
